using GameCore;
using System.IO;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif

public class GlobalConfig : ScriptableObject
{
    public AssetBundleMode Mode = AssetBundleMode.EditorMode;
    public Platform platform;
    public string version = "1.0.0";
    [Tooltip("可寻址资源， 资源加载仅使用资源名，当你使用这个选项时，加载资源时不需要资源路径和后缀；不开启时，使用完整路径加载资源")]
    public bool addressables;
    public int password = 154789548;

    [Header("热更新配置")]
    public string url = "http://192.168.1.5/";
    [Tooltip("http请求超时时间")]
    public float timeout = 15f;
    public string metadataList = "Assets/Arts/Hotfix/MetadataList.bytes";
    public string hotfixDll = "Assets/Arts/Hotfix/Main.dll.bytes";
    public bool checkFileMD5;
    [Tooltip("使用首包下载, 在CDN服务器上会有个所有资源的压缩文件, 当游戏是第一次下载时, 会下载这个压缩文件, 而不是一个个AB文件下载")]
    public bool useFirstPackage = true;
    [Tooltip("首包压缩文件在StreamingAssets路径下，当游戏启动后会在StreamingAssets路径复制到持久化文件夹下，就不需要下载首包文件了")]
    public bool firstPackageInStreamingAssets = false;
    [Tooltip("检查多少个AB包刷新一下Loading界面")]
    public int checkAssetsRefresh = 1;

    [Header("游戏表配置")]
    public string tablePath = "Assets/Arts/Table/GameConfig.bytes";

#if UNITY_EDITOR
    [MenuItem("Assets/GameDesigner/GameCore/Create GlobalConfig")]
    public static void CreateAssetBundleBuilder()
    {
        var globalConfig = CreateInstance<GlobalConfig>();
        globalConfig.platform = (Platform)EditorUserBuildSettings.activeBuildTarget;
        var path = AssetDatabase.GetAssetPath(Selection.activeObject);
        if (string.IsNullOrEmpty(path))
            path = "Assets";
        else if (Path.GetExtension(path) != "")
            path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
        var assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(path + "/GlobalConfig.asset");
        AssetDatabase.CreateAsset(globalConfig, assetPathAndName);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        EditorUtility.FocusProjectWindow();
        Selection.activeObject = globalConfig;
    }
#endif
}